package com.lizonghui.googleplay.control;

import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lizonghui.googleplay.R;
import com.lizonghui.googleplay.base.BaseHolder;
import com.lizonghui.googleplay.bean.ItemInfoBean;
import com.lizonghui.googleplay.config.Constants;
import com.lizonghui.googleplay.utils.UIUtils;
import com.nineoldandroids.animation.ObjectAnimator;
import com.nineoldandroids.animation.ValueAnimator;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import it.sephiroth.android.library.picasso.Picasso;

/**
 * Created by Administrator on 2016/6/25.
 * 模块化视图设计
 * 数据类型就仍是DetailInfoBean
 */
public class DetailSafeHolder extends BaseHolder<ItemInfoBean> implements View.OnClickListener {

    @Bind(R.id.app_detail_safe_iv_arrow)
    ImageView mAppDetailSafeIvArrow;
    @Bind(R.id.app_detail_safe_pic_container)
    LinearLayout mAppDetailSafePicContainer;
    @Bind(R.id.app_detail_safe_des_container)
    LinearLayout mAppDetailSafeDesContainer;

    private Boolean isOpen = true;

    //初始化视图,找到孩子
    @Override
    public View initHolderView() {
        View rootView = View.inflate(UIUtils.getContext(), R.layout.item_detail_safe, null);
        rootView.setOnClickListener(this);//设置点击折叠的监听
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    //绑定数据 data+view
    @Override
    public void refreshHolderView(ItemInfoBean data) {
        List<ItemInfoBean.SafeItemInfoBean> safe = data.safe;//拿到data中的safe集合
        for (ItemInfoBean.SafeItemInfoBean safeItemInfoBean : safe) {//遍历集合取出url之后加载数据
            String safeUrl = safeItemInfoBean.safeUrl;
            ImageView ivIcon = new ImageView(UIUtils.getContext());//创建ImageView来装图标
            Picasso.with(UIUtils.getContext()).load(Constants.URLS.IMABASEURL + safeUrl).into(ivIcon);//加载图片到ImageView
            mAppDetailSafePicContainer.addView(ivIcon);//这个布局容器，我们用动态填充imageView进去

            String safeDesUrl = safeItemInfoBean.safeDesUrl;
            ImageView ivDex = new ImageView(UIUtils.getContext());
            Picasso.with(UIUtils.getContext()).load(Constants.URLS.IMABASEURL + safeDesUrl).into(ivDex);
            LinearLayout linearLayout = new LinearLayout(UIUtils.getContext());
            TextView tv = new TextView(UIUtils.getContext());
            tv.setText(safeItemInfoBean.safeDes);
            //根据返回来的颜色定义位，来给定颜色
            if (safeItemInfoBean.safeDesColor == 0) {
                tv.setTextColor(UIUtils.getColor(R.color.app_detail_safe_normal));
            } else {
                tv.setTextColor(UIUtils.getColor(R.color.app_detail_safe_warning));
            }

            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            params.gravity = Gravity.CENTER;//设置图片效果在中间
            linearLayout.addView(ivDex,params);

            linearLayout.addView(tv);
            int padding = UIUtils.px2dip(4);
            linearLayout.setPadding(padding, padding, padding, padding);
            mAppDetailSafeDesContainer.addView(linearLayout);
        }

        //设置默认是折叠状态mAppDetailSafeDesContainer
        //我们的做法是，在绑定啊数据时候去触发折叠
        //当我们在进去时候发现，我们可以看到折叠的动画，体验不好，所以我们刚进去时候不要动画效果，添加判断位
        toggleAnimation(false);//刚进去不带动画效果
    }

    //切换动画
    private void toggleAnimation(boolean isAnimation) {
        //原来打开-->折叠
        //原来折叠-->打开
        if (isOpen) {
            //折叠-->mAppDetailSafeDesContainer高度 从应有高度 变为0高度
            mAppDetailSafeDesContainer.measure(0,0);//调用measure测量方法去测量该控件(LinearLayout)
            int startHeight = mAppDetailSafeDesContainer.getMeasuredHeight();//取得应有高度
            int endHeight = 0;

            if(isAnimation){
                doAnimation(startHeight,endHeight);//执行动画折叠效果
            }else {
                //直接修改高度
                ViewGroup.LayoutParams layoutParams = mAppDetailSafeDesContainer.getLayoutParams();
                layoutParams.height = endHeight;
                mAppDetailSafeDesContainer.setLayoutParams(layoutParams);
            }
            isOpen = false;
        } else {
            //打开-->mAppDetailSafeDesContainer高度 从0 变为应有高度
            mAppDetailSafeDesContainer.measure(0,0);//调用measure测量方法，去测量该控件
            int startHeight = 0;
            int endHeight = mAppDetailSafeDesContainer.getMeasuredHeight();//取得应有高度

            doAnimation(startHeight,endHeight);//这里不用判断是否需要动画，因为只有进来第一次才不要动画，那时候isOpen为true
            isOpen = true;
        }
    }


    @Override
    public void onClick(View v) {
        toggleAnimation(true);//点击时候带动画效果
    }

    private void doAnimation(int startHeight,int endHeight) {

        //用属性动画 ValueAnimator 和objectAnimator(用它有条件，修改的东西是控件set方法中有的属性)
        ValueAnimator animator = ValueAnimator.ofInt(startHeight,endHeight);
        animator.start();

        //得到动画执行过程的渐变值
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                int height = (int) valueAnimator.getAnimatedValue();
                //得到mAppDetailSafeDesContainer当前layoutParams信息
                ViewGroup.LayoutParams layoutParams = mAppDetailSafeDesContainer.getLayoutParams();
                layoutParams.height = height;
                //设置最新的layoutParams信息
                mAppDetailSafeDesContainer.setLayoutParams(layoutParams);
            }
        });


        //箭头跟着动,就是旋转Rotation。而mAppDetailSafeIvArrow有setRotation。用objectAnimator
        if(isOpen){
            ObjectAnimator.ofFloat(mAppDetailSafeIvArrow,"rotation",180,0).start();
        }else {
            ObjectAnimator.ofFloat(mAppDetailSafeIvArrow,"rotation",0,180).start();
        }
    }
}
